const {as4KDotsTc, as4KDotsBaseInfo} = require("../db/init");
const {Op} = require("../db");
const {isPureChinese, isPureEnglish, isPureNumber} = require("../utils/string_utils");


/**
 * 选出所有题材,或者指定题材的板块的股票
 * @param offset
 * @param limit
 * @param tc_name
 * @returns {Promise<{count: any, rows: any}>}
 */
module.exports.findAll = async function (offset, limit, tc_name) {
    let options = {
        offset: parseInt(offset), limit: parseInt(limit)
    }
    if (tc_name !== undefined) {
        options.where = {
            board_name: {
                [Op.like]: `%${tc_name}%`
            }
        }
    }
    console.log('options', options)
    let {count, rows} = await as4KDotsTc.findAndCountAll(options);

    return {count, rows}
}
// 根据股票代码查找出所有题材
module.exports.findAllByStockCode = async function (stock_code) {
    let options = {
        where: {},
    }
    if (isPureChinese(stock_code)) {
        options.where.security_name = {
            [Op.like]: `%${stock_code}%`
        }
    } else if (isPureEnglish(stock_code)) {
        //从 base表中查找出 security_code
        const code = await as4KDotsBaseInfo.findOne({
            attributes: ['stock_code'],
            where: {
                stock_name_first_letter: {
                    [Op.like]: `%${stock_code}%`
                }
            }
        })
        options.where.security_code = {
            [Op.like]: `%${code.stock_code}%`
        }
    } else if (isPureNumber(stock_code)) {
        options.where.security_code = {
            [Op.like]: `%${stock_code}%`
        }
    } else {
        return {count: 0, rows: []}
    }
    let {count, rows} = await as4KDotsTc.findAndCountAll(options);
    return {count, rows}
}
